/**
 * 多边形绘制
 * @param {Object} ctx 上下文
 * @param {String} color 线条颜色
 * @param {Object} beginDot 起点坐标
 * @param {Object} endDot 终点坐标
 * @author: Huisir
 * @update: 2019-7-16 14:46:07
 */	
var Shape = function (ctx,color,cWidth,cHeight) {
	this.ctx = ctx
	this.color = color
	this.cWidth = cWidth	//绘图区宽
	this.cHeight = cHeight
	//样式
	this.ctx.lineWidth="1";
	this.ctx.strokeStyle = this.color;	//颜色
	//历史
	this.history = []
}

Shape.prototype = {
	/* 多边形 */
	drawPolygon:function(mouseDot,points,flag,beginPoint){
		//清除画布
		this.ctx.clearRect(0,0,this.cWidth,this.cHeight);
		//恢复历史
		this.restore()
		//画多边形
		this.ctx.beginPath();
		//开始绘制
		if(flag==1){
			this.ctx.moveTo(points[0].x , points[0].y);
		}else{
			this.ctx.moveTo(beginPoint.x , beginPoint.y);
		}
		//绘制当前line
		this.ctx.lineTo(mouseDot.x , mouseDot.y);
		//还原多边形
		for (var i = 0; i < points.length; i++) {
			this.ctx.lineTo(points[i].x , points[i].y);
		}
		//闭合
		this.ctx.closePath();
		//渲染
		this.ctx.stroke();
	},
	/* 圆 */
	drawCircle:function(centerDot,mouseDot) {
		//求半径/两点距离
		var r = Math.sqrt((mouseDot.x - centerDot.x) * (mouseDot.x - centerDot.x) + (mouseDot.y - centerDot.y) * (mouseDot.y - centerDot.y))
		//清除画布
		this.ctx.clearRect(0,0,this.cWidth,this.cHeight);
		//恢复历史
		this.restore()
		this.ctx.beginPath();
		this.ctx.arc(centerDot.x,centerDot.y,r,0,2*Math.PI);
		this.ctx.closePath(); //闭合
		this.ctx.stroke();
	},
	/* 还原历史 */
	restore:function() {
		for (var i = 0; i < this.history.length; i++) {
			this.ctx.putImageData(this.history[i] , 0, 0);
		}
	}
}